#include "qbackgroundwidget.h"
#include "ui_qbackgroundwidget.h"

#include "../qimagechoosedialog.h"
#include "../qsoftcore.h"
#include "../../sharedLib/qresourcemanager.h"

#include "../../gradientEditor/qtgradientdialog.h"
#include "../../sharedLib/qprojectresource.h"
#include "../../sharedLib/stylesheet/qgradientmaker.h"

#include <QColorDialog>

using namespace CORELIB;

QBackgroundWidget::QBackgroundWidget(QWidget *parent):
    QAbstractStylesheetWidget(parent),
    ui(new Ui::QBackgroundWidget)
{
    ui->setupUi(this);
    this->setProperty("no-ManhattanStyle",true);
    this->setFixedHeight(this->height());

    ui->comboBox->addItems(QStringList()<<"repeat-x"<<"repeat-y"<<"repeat-xy"<<"no-repeat");
    connect(ui->comboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(repeat(QString)));
    ui->comboBox->setCurrentIndex(2);

    ui->comboBox_2->addItems(QStringList()<<"left top"<<"left center"<<"left bottom"<<"center top"<<"center"<<"center bottom"
                             <<"right top"<<"right center"<<"right bottom");
    connect(ui->comboBox_2,SIGNAL(currentIndexChanged(QString)),this,SLOT(position(QString)));
    ui->comboBox_2->setCurrentIndex(0);
}

QBackgroundWidget::~QBackgroundWidget()
{
    delete ui;
    if(m_tempResource!="")
    {
        SHAREDLIB::QProjectResource::resource()->removeResource(m_tempResource);
    }
}

void QBackgroundWidget::on_pushButton_clicked()
{
    QColor c=QColorDialog::getColor(Qt::white,this);
    if(c.isValid())
    {
        QGradient g=QLinearGradient(0,0,1,0);
        g.setColorAt(0,c);
        g.setColorAt(1,c);
        QVariant v;
        v.setValue<QGradient>(g);
        m_item.m_attributes.insert("color",v);
        makeStylesheet();
    }
}

void CORELIB::QBackgroundWidget::on_pushButton_2_clicked()
{
    QtGradientDialog dlg(this);
    QVariant v;
    v=m_item.m_attributes.value("color");
    dlg.setGradient(v.value<QGradient>());
    int res=dlg.exec();
    if (res == QDialog::Accepted)
    {
        v.setValue<QGradient>(dlg.gradient());
        m_item.m_attributes.insert("color",v);
        makeStylesheet();
    }
}

void CORELIB::QBackgroundWidget::on_pushButton_3_clicked()
{
    QImageChooseDialog dlg(this);
    dlg.exec();
    QString first,last;
    dlg.getImage(first,last);

    if(first!="" && last!="")
    {
        if(last!=m_item.m_attributes.value("image").toString())
        {
            QByteArray data=QSoftCore::getCore()->resourceManager()->resourceData(first,last);
            if(data.size()>0)
            {
                SHAREDLIB::QProjectResource::resource()->addResource(last,data);
                m_item.m_attributes.insert("image",last);
                if(m_tempResource!="")
                {
                    SHAREDLIB::QProjectResource::resource()->removeResource(m_tempResource);
                }
                m_tempResource=last;
                makeStylesheet();
            }
        }
    }
}

void QBackgroundWidget::repeat(const QString &text)
{
    QString str=text;
    if(m_item.m_attributes.value("repeat").toString()!=str)
    {
        m_item.m_attributes.insert("repeat",str);
        makeStylesheet();
    }
}

void QBackgroundWidget::position(const QString &text)
{
    QString str=text;
    if(m_item.m_attributes.value("position").toString()!=str)
    {
        m_item.m_attributes.insert("position",str);
        makeStylesheet();
    }
}

void QBackgroundWidget::takeResource()
{
    m_tempResource="";
}

void QBackgroundWidget::setItem(const tagStylesheetItem &item)
{
    QString str;

    str=item.m_attributes.value("position","left top").toString();
    ui->comboBox_2->setCurrentIndex(ui->comboBox_2->findText(str));

    str=item.m_attributes.value("repeat","repeat-xy").toString();
    ui->comboBox->setCurrentIndex(ui->comboBox->findText(str));
    QAbstractStylesheetWidget::setItem(item);
}
